package ua.org.tumakha.resume;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ua.org.tumakha.resume.xml.XMLValidator;
import ua.org.tumakha.resume.xml.transform.FoTransformer;
import ua.org.tumakha.resume.xml.transform.XmlTransformer;
import ua.org.tumakha.resume.xml.transform.XslTransformer;

/**
 * @author Yuriy Tumakha
 */
public class ResumeGenerator {

    protected static final Log log = LogFactory.getLog(ResumeGenerator.class);
    private File htmlStyleSheet;
    private File foStyleSheet;
    private File outputDir;
    private List<File> xmlSources = new ArrayList<File>();

    public void setHtmlStyleSheet(File htmlStyleSheet) {
        this.htmlStyleSheet = htmlStyleSheet;
    }

    public File getHtmlStyleSheet() {
        return htmlStyleSheet;
    }

    public void setFoStyleSheet(File foStyleSheet) {
        this.foStyleSheet = foStyleSheet;
    }

    public File getFoStyleSheet() {
        return foStyleSheet;
    }

    public void setOutputDir(File outputDir) {
        this.outputDir = outputDir;
    }

    public File getOutputDir() {
        return outputDir;
    }

    public void addXmlSource(File xmlSource) throws Exception {
//        if (XMLValidator.parse(xmlSource)) {
            xmlSources.add(xmlSource);
//        }
    }

    public static void main(String[] args) throws Exception {
        ResumeGenerator resumeGenerator = new ResumeGenerator();
        resumeGenerator.setHtmlStyleSheet(new File("stylesheets/resume2html.xsl"));
        resumeGenerator.setFoStyleSheet(new File("stylesheets/resume2fo.xsl"));
        resumeGenerator.addXmlSource(new File("resources/resume_ru.xml"));
        resumeGenerator.addXmlSource(new File("resources/resume_en.xml"));
        resumeGenerator.setOutputDir(new File("target/resume/"));
        log.info("Start generating...");
        resumeGenerator.generate();
        log.info("All files generated.");
    }

    public void generate() throws Exception {
        File outputDir = getOutputDir();
        if (!outputDir.exists()) {
            outputDir.mkdirs();
        }
        XslTransformer.setOutputDirectory(outputDir);
        for (XmlTransformer transformer : getTransformers()) {
            for (File xmlSource : xmlSources) {
                transformer.transform(xmlSource);
            }
        }
    }

    private List<XmlTransformer> getTransformers() throws Exception {
        List<XmlTransformer> transformers = new ArrayList<XmlTransformer>();
        transformers.add(new XslTransformer(getHtmlStyleSheet(), ".html"));
        if (log.isDebugEnabled()) {
            transformers.add(new XslTransformer(getFoStyleSheet(), ".fo.xml"));
        }
        transformers.add(new FoTransformer(getFoStyleSheet(), ".pdf"));
        transformers.add(new FoTransformer(getFoStyleSheet(), ".rtf"));
        return transformers;
    }

}
